//
//  MainTableViewCell.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/24.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MainTableViewCell.h"

#import <SDAutoLayout.h>
#import <SDWebImage/UIImageView+WebCache.h>

@implementation MainTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
            [self.contentView addSubview:self.img];
            [self.contentView addSubview:self.title];
            [self.contentView addSubview:self.typeLbl];
            [self.contentView addSubview:self.bottomLine];
            [self layoutViews];
    }
    return self;
}

- (UIImageView *)img {
    if (!_img) {
        _img = [[UIImageView alloc] init];
    }
    return _img;
}

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.font = [UIFont systemFontOfSize:21];
    }
    return _title;
}

- (UILabel *)typeLbl {
    if (!_typeLbl) {
        _typeLbl = [[UILabel alloc] init];
        _typeLbl.backgroundColor = [UIColor colorWithRed:(226/255.0) green:(124/255.0) blue:(42/255.0) alpha:1];
        _typeLbl.text = @"推荐";
        _typeLbl.textColor = [UIColor whiteColor];
        _typeLbl.textAlignment = NSTextAlignmentCenter;
        _typeLbl.layer.cornerRadius = 8;
        _typeLbl.layer.masksToBounds = YES;
    }
    return _typeLbl;
}

- (UILabel *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UILabel alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithRed:(235/255.0) green:(235/255.0) blue:(235/255.0) alpha:1];
    }
    return _bottomLine;
}

- (void)layoutViews {
    
    _img.sd_layout
    .topSpaceToView(self.contentView,0)
    .leftSpaceToView(self.contentView,0)
    .heightIs([UIScreen mainScreen].bounds.size.height / 3)
    .rightSpaceToView(self.contentView,0);
    
    _typeLbl.sd_layout
    .topSpaceToView(_img,15)
    .leftSpaceToView(self.contentView,15)
    .widthIs(50)
    .heightIs(30);
    
    _title.sd_layout
    .topSpaceToView(_img,15)
    .leftSpaceToView(_typeLbl,10)
    .rightSpaceToView(self.contentView,15)
    .autoHeightRatio(0);
    
    _bottomLine.sd_layout
    .topSpaceToView(_title,15)
    .leftSpaceToView(self.contentView,0)
    .rightSpaceToView(self.contentView,0)
    .heightIs(10);
    
    [self setupAutoHeightWithBottomView:_bottomLine bottomMargin:0];
}

- (void)setModel:(HomeModel_Product *)model {
    [_img sd_setImageWithURL:[NSURL URLWithString:model.img_bit] placeholderImage:nil];
    _title.text = model.name;
}

@end


